package com.jiesen.base.modules.certificate.jsUser.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.certificate.jsUser.model.JsUserDTO;
import com.jiesen.base.modules.certificate.jsUser.model.JsUserDetail;
import com.jiesen.base.modules.certificate.jsUser.service.IJsUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiesen.base.baseDao.BaseServiceImpl;
import com.jiesen.base.modules.certificate.jsUser.mapper.JsUserMapper;
import com.jiesen.base.modules.certificate.jsUser.model.JsUser;
import com.jiesen.base.modules.certificate.jsUser.service.IJsUserService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bao
 */
@Service
public class JsUserServiceImpl extends BaseServiceImpl<JsUserMapper, JsUser>implements IJsUserService {

    @Autowired
    private IJsUserDetailService jsUserDetailService;

    @Override
    @Transactional
    public boolean save(JsUserDTO jsUserDTO) {
        jsUserDTO.setUts(LocalDateTime.now());
        jsUserDTO.setCts(LocalDateTime.now());
        JsUser jsUser = jsUserDTO.getJsUser();
        JsUserDetail jsUserDetail = jsUserDTO.getJsUserDetail();
        try {
            baseMapper.insert(jsUser);
            jsUserDetail.setUserMainId(jsUser.getId());
            jsUserDetailService.save(jsUserDetail);
        } catch (Exception e) {
            System.out.println(e.toString());
            throw new RuntimeException("添加失败");
        }
        return true;
    }

    @Override
    @Transactional
    public boolean update(JsUserDTO jsUserDTO) {
        jsUserDTO.setCts(LocalDateTime.now());
        JsUser jsUser = jsUserDTO.getJsUser();
        JsUserDetail jsUserDetail = jsUserDTO.getJsUserDetail();
        try {
            baseMapper.updateById(jsUser);
            QueryWrapper<JsUserDetail> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_main_id",jsUser.getId());
            jsUserDetailService.update(jsUserDetail,queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException("更新失败");
        }
        return true;
    }

    @Override
    @Transactional
    public boolean delete(String id) {
        try {
            baseMapper.deleteById(id);
            QueryWrapper<JsUserDetail> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_main_id",id);
            jsUserDetailService.remove(queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException("删除失败");
        }
        return true;
    }

    @Override
    public JsUser findUserByTelphone(String telphone) {
        QueryWrapper<JsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telphone",telphone);
        List<JsUser> jsUsers = baseMapper.selectList(queryWrapper);
        if(jsUsers==null){
            return null;
        }
        return jsUsers.get(0);
    }
}
